using System.Collections;
using FireStarter.Base;
using FireStarterModeller.BO;
using FireStarterModeller.Logic;
using FireStarterModeller.Test.BO;
using Habanero.Base;
using Habanero.Base.Exceptions;
using Habanero.BO.ClassDefinition;
using Habanero.BO.Loaders;
using NUnit.Framework;

namespace FireStarterModeller.Test.Logic
{
    [TestFixture]
    public class TestCreateUIFormForDMRelationship
    {
        #region Setup/Teardown

        [TestFixtureSetUp]
        public virtual void TestFixtureSetup()
        {
            TestUtilsLogic.SetupFixture();
        }

        [SetUp]
        public virtual void SetupTest()
        {
            TestUtilsLogic.SetupTest();
            ClassDef.ClassDefs.Clear();
            ClassDef.ClassDefs.Add(new XmlClassDefsLoader(BOBroker.GetClassDefsXml(), new DtdLoader()).LoadClassDefs());
        }

        [TearDown]
        public virtual void TearDownTest()
        {
        }

        #endregion

        [Test]
        public void Test_GetDefaultUIDef_ShouldBeSetupWithRelatedClassIDProperty()
        {
            //---------------Set up test pack-------------------
            ClassDef dmClassDef = ClassDef.Get<DMRelationship>();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmClassDef);
            //---------------Execute Test ----------------------
            IUIDef uiDef = dmClassDef.GetUIDef("default");
            //---------------Test Result -----------------------
            Assert.IsNotNull(uiDef);
            Assert.IsNotNull(uiDef.UIGrid);
            Assert.IsNotNull(uiDef.UIForm);
            Assert.AreEqual(1, uiDef.UIForm.Count);
            IUIFormTab formTab = uiDef.UIForm[0];
            Assert.IsNotNull(formTab);
            Assert.AreEqual(1, formTab.Count);
            IUIFormColumn formColumn = formTab[0];
            Assert.IsNotNull(formColumn);
            Assert.GreaterOrEqual(formColumn.Count, 1);
            IUIFormField formField = formColumn[0];
            Assert.IsNotNull(formField);
            Assert.AreEqual("ReverseRelationship.ClassDM", formField.PropertyName);
        }

        [Test]
        public void Test_RemoveFormField_ShouldRemoveFromColumn()
        {
            //---------------Set up test pack-------------------
            ClassDef dmClassDef = ClassDef.Get<DMRelationship>();
            IUIDef uiDef = dmClassDef.GetUIDef("default");
            IUIFormField formField = uiDef.GetFormField("ReverseRelationship.ClassDM");
            IUIFormColumn formColumn = formField.UIFormColumn;
            int origFormFieldCount = formColumn.Count;
            //---------------Assert Precondition----------------
            Assert.IsNotNull(formColumn);
            //---------------Execute Test ----------------------
            formColumn.Remove(formField);
            //---------------Test Result -----------------------
            Assert.AreEqual(origFormFieldCount - 1, formColumn.Count);
        }

        [Test]
        public void Test_AddFormFieldBack_ShouldAddToColumn()
        {
            //---------------Set up test pack-------------------
            ClassDef dmClassDef = ClassDef.Get<DMRelationship>();
            IUIDef uiDef = dmClassDef.GetUIDef("default");
            IUIFormField formField = uiDef.GetFormField("ReverseRelationship.ClassDM");
            IUIFormColumn formColumn = formField.UIFormColumn;
            int origFormFieldCount = formColumn.Count;
            formColumn.Remove(formField);
            //---------------Assert Precondition----------------
            Assert.IsNotNull(formColumn);
            Assert.AreEqual(origFormFieldCount - 1, formColumn.Count);
            //---------------Execute Test ----------------------
            formColumn.Add(formField);
            //---------------Test Result -----------------------
            Assert.AreEqual(origFormFieldCount, formColumn.Count);
            Assert.AreSame(formField, formColumn[origFormFieldCount - 1]);
        }

        [Test]
        public void Test_AddFormFieldToBeginning_ShouldBeFirstItemToColumn()
        {
            //---------------Set up test pack-------------------
            ClassDef dmClassDef = ClassDef.Get<DMRelationship>();
            IUIDef uiDef = dmClassDef.GetUIDef("default");
            IUIFormField formField = uiDef.GetFormField("ReverseRelationship.ClassDM");
            IUIFormColumn formColumn = formField.UIFormColumn;
            int origFormFieldCount = formColumn.Count;
            formColumn.Remove(formField);

            UIFormField newFormField = GetNewFormField();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(formColumn);
            Assert.AreEqual(origFormFieldCount - 1, formColumn.Count);
            //---------------Execute Test ----------------------
            formColumn.Insert(0, newFormField);
            //---------------Test Result -----------------------
            Assert.AreEqual(origFormFieldCount, formColumn.Count);
            Assert.AreSame(newFormField, formColumn[0]);
        }

        private static UIFormField GetNewFormField()
        {
            string randomString = TestUtilsShared.GetRandomString();
            return new UIFormField
                (null, randomString, randomString, randomString, randomString, randomString, false, false, randomString,
                 new Hashtable(), new LayoutStyle());
        }
    }
}